package it.univaq.mwt.arssa.presentation.startup;

import it.univaq.mwt.arssa.business.ArssaBusinessFactory;
import it.univaq.mwt.arssa.business.impl.JDBCArssaBusinessFactory;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;

import javax.naming.Context;

public class ArssaServletContextListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		
		try {
			Context initCtx = (Context) new InitialContext();
	        Context envCtx = (Context) ((InitialContext) initCtx).lookup( "java:comp/env" );
	        DataSource dataSource = (DataSource) envCtx.lookup( "jdbc/arssaDB" );		
	        ArssaBusinessFactory.setInstance(new JDBCArssaBusinessFactory(dataSource));
			
		} catch (NamingException e) {
			e.printStackTrace();
		}		
		
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
			ArssaBusinessFactory.setInstance(null);	
	}

}
